<template>
    <div ref="chartBarRef">
        <el-card>
            <div class="flex flex-jc-sb">
                <div class="fs-14 bold">{{ title }}</div>
                <div class="flex items-center">
                    <!-- <el-select v-model="selectVal" class="m-2" placeholder="请选择" size="small">
                        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
                    </el-select> -->
                    <customer-switch v-model:active="active" class="mg-l-20" :list="list"
                        @change="handleSwitchChange"></customer-switch>
                </div>
            </div>
            <div class="chart-container" ref="chartRef">
            </div>
        </el-card>
    </div>
</template>
<script setup>
import { ref, computed, watch, onMounted, onActivated } from 'vue'
import * as echarts from 'echarts'

import { getChartData } from '@/api/baobai/dyData.ts'

const props = defineProps({
    title: {
        type: String,
        default: '统计分析'
    },
    showLegend: {
        type: Boolean,
        default: false
    },
    type: {
        type: String,
        default: 'sale'
    },
    insureType: {
        type: String,
        default: 'eleSign'
    },
    queryData: {
        type: Object,
        default() {
            return {}
        }
    }
})

const active = ref(0)
const list = ref([
    {
        name: '本周',
        value: 0
    },
    {
        name: '本月',
        value: 1
    },
])

const handleChange = () => {
    console.log(111)
}

const handleSwitchChange = () => {
    getData()
}

const chartRef = ref(null)
const chartBarRef = ref(null)
let myChart = null

const createChartOption = (data) => {
    const chartOption =
    {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        legend: {
            // data: ['曝光量', '点击量', '拨号量', '成交量', '成交金额']
            data: ['曝光量', '拨号量', '成交量']
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: data.statisticsDate,
            axisLine: {
                show: true,
                lineStyle: {
                    color: '#EAEAEF',
                    type: 'solid'
                },
            },
            axisLabel: {
                show: true,
                color: '#9497B1',
                margin: 20
            }
        },
        yAxis: {
            type: 'value',
            boundaryGap: [0, '100%'],
            splitLine: {
                lineStyle: {
                    type: 'dashed'
                }
            },
            axisLabel: {
                show: true,
                color: '#9497B1'
            }
        },
        grid: {
            left: 40,
            right: 40,
            top: 30,
            bottom: 30,
        },
        series: [
            {
                name: '曝光量',
                type: 'line',
                // stack: '总量',
                smooth: true,
                lineStyle: {
                    width: 3,
                    shadowColor: 'rgba(2, 86, 255, 0.3)',
                    shadowBlur: 10,
                    shadowOffsetY: 5,
                    color: '#0256FF'
                },
                itemStyle: {
                    color: '#0256FF'
                },
                data: data.exposureCount
            },
            // {
            //     name: '点击量',
            //     type: 'line',
            //     stack: '总量',
            //     smooth: true,
            //     lineStyle: {
            //         width: 3,
            //         shadowColor: 'rgba(255, 118, 66, 0.4)',
            //         shadowBlur: 10,
            //         shadowOffsetY: 5,
            //         color: '#FF7642'
            //     },
            //     itemStyle: {
            //         color: '#FF7642'
            //     },
            //     data: [220, 182, 191, 234, 290, 330, 310, 182, 191, 234, 290, 330]
            // },
            {
                name: '拨号量',
                type: 'line',
                // stack: '总量',
                smooth: true,
                lineStyle: {
                    width: 3,
                    shadowColor: 'rgba(0, 214, 121, 0.4)',
                    shadowBlur: 10,
                    shadowOffsetY: 5,
                    color: '#00D679'
                },
                itemStyle: {
                    color: '#00D679'
                },
                data: data.callCount
            },
            {
                name: '成交量',
                type: 'line',
                // stack: '总量',
                smooth: true,
                lineStyle: {
                    width: 3,
                    shadowColor: 'rgba(111, 66, 251, 0.4)',
                    shadowBlur: 10,
                    shadowOffsetY: 5,
                    color: '#6F42FB'
                },
                itemStyle: {
                    color: '#6F42FB'
                },
                data: data.dealCount
            },
            // {
            //     name: '成交金额',
            //     type: 'line',
            //     stack: '总量',
            //     smooth: true,
            //     lineStyle: {
            //         width: 3,
            //         shadowColor: 'rgba(30, 231, 255, 0.4)',
            //         shadowBlur: 10,
            //         shadowOffsetY: 5,
            //         color: '#1EE7FF'
            //     },
            //     itemStyle: {
            //         color: '#1EE7FF'
            //     },
            //     data: [820, 932, 901, 934, 1290, 1330, 1320, 932, 901, 934, 1290, 1330]
            // }
        ]
    }
    // chartOption.series[0].name = seriesMap[active.value]
    if (props.showLegend) {
        chartOption.legend = {
            right: 0,
            top: 5
        }
    }
    return chartOption
}
let chartData = {}

const setOption = () => {
    myChart.setOption(createChartOption(chartData))
}

const getData = () => {
    getChartData({
        type: active.value
    }).then(res => {
        chartData = res
        setOption()
    })
}

const resize = () => {
    myChart.resize()
}


watch(() => props, (newval) => {
    getData()
}, {
    immediate: true,
    deep: true
})

defineExpose({
    chartBarRef,
    resize,
    getData
})

onMounted(() => {
    myChart = echarts.init(chartRef.value)
    setOption()
})

</script>
<style lang="scss" scoped>
.chart-container {
    width: 100%;
    height: calc(100vh - 430px);
}
</style>